package com.demo.mybaits.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import com.demo.mybaits.model.Websites;
import com.demo.mybaits.model.dao.WebsitesMapper;
import com.demo.mybaits.service.WebsitesService;
import com.demo.mybaits.sqlsession.SqlSessionProvider;

@Service
public class WebsitesServiceImpl implements WebsitesService {

	/*
	 * @Autowired private WebsitesMapper websitesMapper;
	 */

	// springBoot自动集成mybaits事务处理
	/*
	 * @Transactional(rollbackFor = Exception.class)
	 * 
	 * @Override public void insert(Websites websites) throws Exception {
	 * websitesMapper.insert(websites);
	 * 
	 * }
	 */

	// 使用自定义配置的mybaits事务处理
	/*
	 * @Override public void insert(Websites websites) { SqlSession sqlSession =
	 * null; try { // 打开 SqlSession 会话 sqlSession =
	 * SqlSessionProvider.INSTANCE.init().openSession();
	 * websitesMapper.insert(websites); sqlSession.commit(); // 提交事务
	 * log.info("写入数据库成功"); } catch (Exception e) { log.error("写入数据库异常");
	 * sqlSession.rollback(); // 回滚事务 }finally{ // 在 finally 语句中确保资源被顺利关闭
	 * if(sqlSession != null){ sqlSession.close(); } }
	 * 
	 * }
	 */

	// 配置了全局事务管理过滤器
	@Override
	public void insert(Websites websites) {
		SqlSession sqlSession = SqlSessionProvider.INSTANCE.getSession();
		WebsitesMapper websitesMapper = sqlSession.getMapper(WebsitesMapper.class);
		System.out.println("插入");
		websitesMapper.insert(websites);
		
		//throw new RuntimeException("cccc");
	}

}
